﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangMayTinh.DataAccessLayer;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.BusinessLogicLayer
{
    /// <summary>
    /// Lớp MatHangManager
    /// </summary>
    public class MatHangManager
    {
        /// <summary>
        /// Khởi tạo đối tượng MatHangDA
        /// </summary>
        private static MatHangDA adapter = null;

        static MatHangManager()
        {
            if (adapter == null)
                adapter = new MatHangDA();
        }

        /// <summary>
        /// Hàm thêm 1 mặt hàng mới
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static int Insert(MatHangEntity entity)
        {
            try
            {
                return adapter.Insert(entity);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// Hàm thêm 1 mặt hàng mới
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static int Insert(MatHangEntity entity, ref string errormessage)
        {
            try
            {
                return adapter.Insert(entity);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// Hàm thêm 1 mặt hàng mới lấy về ID
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static int InsertGetID(MatHangEntity entity)
        {
            try
            {
                return adapter.InsertGetID(entity);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// Hàm thêm 1 mặt hàng mới lấy về ID
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static int InsertGetID(MatHangEntity entity, ref string errormessage)
        {
            try
            {
                return adapter.InsertGetID(entity);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// Hàm cập nhật 1 mặt hàng mới
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool UpDate(MatHangEntity entity)
        {
            try
            {
                return adapter.UpDate(entity);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// Hàm cập nhật 1 mặt hàng mới
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static bool UpDate(MatHangEntity entity, ref string errormessage)
        {
            try
            {
                return adapter.UpDate(entity);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// Hàm cập nhật tình trạng của 1 mặt hàng
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool Update_TinhTrang(MatHangEntity entity)
        {
            try
            {
                return adapter.Update_TinhTrang(entity);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// Hàm cập nhật tình trạng của 1 mặt hàng
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static bool Update_TinhTrang(MatHangEntity entity, ref string errormessage)
        {
            try
            {
                return adapter.Update_TinhTrang(entity);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool Update_Serial(MatHangEntity entity)
        {
            try
            {
                return adapter.Update_Serial(entity);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static bool Update_Serial(MatHangEntity entity, ref string errormessage)
        {
            try
            {
                return adapter.Update_Serial(entity);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// Hàm Lấy tất cả thông tin của mặt hàng
        /// </summary>
        /// <param name="View"></param>
        /// <returns></returns>
        public static DataTable Find_All(bool View)
        {
            try
            {
                return adapter.Find_All(View);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm Lấy tất cả thông tin của mặt hàng
        /// </summary>
        /// <param name="View"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_All(bool View, ref string errormessage)
        {
            try
            {
                return adapter.Find_All(View);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Tìm mặt hàng bẳng serial của nó
        /// </summary>
        /// <param name="seiral"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public static DataTable Find_Serial(string seiral, bool View)
        {
            try
            {
                return adapter.Find_Serial(seiral, View);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Tìm mặt hàng bẳng serial của nó
        /// </summary>
        /// <param name="seiral"></param>
        /// <param name="View"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_Serial(string seiral, bool View, ref string errormessage)
        {
            try
            {
                return adapter.Find_Serial(seiral, View);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng bằng ID
        /// </summary>
        /// <param name="id"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public static DataTable Find_ID(int id, bool View)
        {
            try
            {
                return adapter.Find_ID(id, View);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng bằng ID
        /// </summary>
        /// <param name="id"></param>
        /// <param name="View"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_ID(int id, bool View, ref string errormessage)
        {
            try
            {
                return adapter.Find_ID(id, View);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo nhà cung cấp
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DataTable Find_NhaCungCap(int id)
        {
            try
            {
                return adapter.Find_NhaCungCap(id);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo nhà cung cấp
        /// </summary>
        /// <param name="id"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_NhaCungCap(int id, ref string errormessage)
        {
            try
            {
                return adapter.Find_NhaCungCap(id);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo hóa đơn bán hàng
        /// </summary>
        /// <param name="MaHoaDonBan"></param>
        /// <returns></returns>
        public static DataTable Find_MaHoaDonBan(int MaHoaDonBan)
        {
            try
            {
                return adapter.Find_MaHoaDonBan(MaHoaDonBan);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo hóa đơn bán hàng
        /// </summary>
        /// <param name="MaHoaDonBan"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_MaHoaDonBan(int MaHoaDonBan, ref string errormessage)
        {
            try
            {
                return adapter.Find_MaHoaDonBan(MaHoaDonBan);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo mã hóa đơn nhập hàng
        /// </summary>
        /// <param name="MaHoaDonNhap"></param>
        /// <returns></returns>
        public static DataTable Find_MaHoaDonNhap(int MaHoaDonNhap)
        {
            try
            {
                return adapter.Find_MaHoaDonNhap(MaHoaDonNhap);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo mã hóa đơn nhập hàng
        /// </summary>
        /// <param name="MaHoaDonNhap"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_MaHoaDonNhap(int MaHoaDonNhap, ref string errormessage)
        {
            try
            {
                return adapter.Find_MaHoaDonNhap(MaHoaDonNhap);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        ///  Hàm tìm mặt hàng theo phiếu xuất kho trả hàng
        /// </summary>
        /// <param name="MaPhieuXuatKhoTraHang"></param>
        /// <returns></returns>
        public static DataTable Find_MaPhieuXuatKhoTraHang(int MaPhieuXuatKhoTraHang)
        {
            try
            {
                return adapter.Find_MaPhieuXuatKhoTraHang(MaPhieuXuatKhoTraHang);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        ///  Hàm tìm mặt hàng theo phiếu xuất kho trả hàng
        /// </summary>
        /// <param name="MaPhieuXuatKhoTraHang"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_MaPhieuXuatKhoTraHang(int MaPhieuXuatKhoTraHang, ref string errormessage)
        {
            try
            {
                return adapter.Find_MaPhieuXuatKhoTraHang(MaPhieuXuatKhoTraHang);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo phiếu bảo hành sửa chữa
        /// </summary>
        /// <param name="MaPhieuBaoHanhSuaChua"></param>
        /// <returns></returns>
        public static DataTable Find_MaPhieuBaoHanhSuaChua(int MaPhieuBaoHanhSuaChua)
        {
            try
            {
                return adapter.Find_MaPhieuBaoHanhSuaChua(MaPhieuBaoHanhSuaChua);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo phiếu bảo hành sửa chữa
        /// </summary>
        /// <param name="MaPhieuBaoHanhSuaChua"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_MaPhieuBaoHanhSuaChua(int MaPhieuBaoHanhSuaChua, ref string errormessage)
        {
            try
            {
                return adapter.Find_MaPhieuBaoHanhSuaChua(MaPhieuBaoHanhSuaChua);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo mã loại mặt hàng của mặt hàng đó
        /// </summary>
        /// <param name="MaLoaiMH"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public static DataTable Find_MaLoaiMH(int MaLoaiMH, bool View)
        {
            try
            {
                return adapter.Find_MaLoaiMH(MaLoaiMH, View);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo mã loại mặt hàng của mặt hàng đó
        /// </summary>
        /// <param name="MaLoaiMH"></param>
        /// <param name="View"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_MaLoaiMH(int MaLoaiMH, bool View, ref string errormessage)
        {
            try
            {
                return adapter.Find_MaLoaiMH(MaLoaiMH, View);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        ///  Hàm tìm mặt hàng theo tên loại mặt hàng
        /// </summary>
        /// <param name="TenLoaiMH"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public static DataTable Find_TenLoaiMH(string TenLoaiMH, bool View)
        {
            try
            {
                return adapter.Find_TenLoaiMH(TenLoaiMH, View);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        ///  Hàm tìm mặt hàng theo tên loại mặt hàng
        /// </summary>
        /// <param name="TenLoaiMH"></param>
        /// <param name="View"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_TenLoaiMH(string TenLoaiMH, bool View, ref string errormessage)
        {
            try
            {
                return adapter.Find_TenLoaiMH(TenLoaiMH, View);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo mã model mặt hàng
        /// </summary>
        /// <param name="MaModelMH"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public static DataTable Find_MaModelMH(int MaModelMH, bool View)
        {
            try
            {
                return adapter.Find_MaModelMH(MaModelMH, View);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm tìm mặt hàng theo mã model mặt hàng
        /// </summary>
        /// <param name="MaModelMH"></param>
        /// <param name="View"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_MaModelMH(int MaModelMH, bool View, ref string errormessage)
        {
            try
            {
                return adapter.Find_MaModelMH(MaModelMH, View);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaModelMH"></param>
        /// <param name="Serial"></param>
        /// <param name="View"></param>
        /// <returns></returns>
        public static DataTable Find_Serial_Model(int MaModelMH, string Serial, bool View)
        {
            try
            {
                return adapter.Find_Serial_Model(MaModelMH, Serial, View);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaModelMH"></param>
        /// <param name="Serial"></param>
        /// <param name="View"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static DataTable Find_Serial_Model(int MaModelMH, string Serial, bool View, ref string errormessage)
        {
            try
            {
                return adapter.Find_Serial_Model(MaModelMH, Serial, View);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaMH"></param>
        /// <param name="Kieu">0: count all</param>
        /// <param name="Kieu">1: count BH</param>
        /// <param name="Kieu">2: count SC</param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static int Find_Serial_Model(int MaMH, int Kieu, ref string errormessage)
        {
            try
            {
                return adapter.Count_BHSC(MaMH, Kieu);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaMH"></param>
        /// <param name="Kieu">0: count all</param>
        /// <param name="Kieu">1: count BH</param>
        /// <param name="Kieu">2: count SC</param>
        /// <returns></returns>
        public static int Count_BHSC(int MaMH, int Kieu)
        {
            try
            {
                return adapter.Count_BHSC(MaMH, Kieu);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaKH"></param>
        /// <returns></returns>
        public static DataTable Find_MaKhachHang(int MaKH, bool View)
        {
            try
            {
                return adapter.Find_MaKhachHang(MaKH, View);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// Hàm xóa 1 mặt hàng
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Delete(int id)
        {
            try
            {
                return adapter.Delete(id);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// Hàm xóa 1 mặt hàng
        /// </summary>
        /// <param name="id"></param>
        /// <param name="errormessage"></param>
        /// <returns></returns>
        public static bool Delete(int id, ref string errormessage)
        {
            try
            {
                return adapter.Delete(id);
            }
            catch (Exception ex)
            {
                errormessage = ex.Message;
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// Hàm chuyển DataTable thành 1 List MatHangEntity
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<MatHangEntity> ConvertToList(DataTable dt)
        {
            try
            {
                return adapter.ConvertToList(dt);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }
    }
}
